package exercises;

import java.util.ArrayList;

/**
 * Created by lauraionescu on 10/04/2014.
 */
public class PrimeFactors {
    public static void main(String[] args){

        ArrayList<Integer> primeFacts = generate(30);
        for (int i = 0; i < primeFacts.size(); i++){
            System.out.print(primeFacts.get(i) + " ");
        }

    }

    public static ArrayList<Integer> generate(int n){
        ArrayList<Integer> primes = new ArrayList<Integer>();

        for (int i = 2; i <= n; i++){
            if (n % i == 0)
                if (isPrime(i))
                    primes.add(i);
        }

        return primes;
    }


    public static boolean isPrime(int n){
        if (n == 2)
            return true;

        for (int i = 2; i <= (int) Math.sqrt(n); i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }
}
